define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goodsrecord/index',
                    add_url: 'goodsrecord/add',
                    edit_url: 'goodsrecord/edit',
                    del_url: 'goodsrecord/del',
                    multi_url: 'goodsrecord/multi',
                    table: 'goods_record',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'goods.goods_name', title: __('Goods_id'), operate: 'LIKE %...%'},
                        {field: 'total_number', title: __('Total_number')},
                        {field: 'buy_number', title: __('Buy_number')},
                        {field: 'status_text', title: __('Status'), operate: false},
                        {field: 'status', title: __('Status'), visible: false, searchList: Config.statusList},
                        {field: 'winning_id', title: __('Winning_id')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'open_time', title: __('Open_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'is_robot', title: '指定开奖', visible: false, searchList: Config.robotList},
                        {field: 'is_robot_text', title: '指定开奖', operate: false},
                        {
                            field: 'operate', 
                            title: __('Operate'), 
                            table: table, 
                            buttons: [
                                {name: 'robot', text:'指定开奖', title: '指定开奖', classname:'btn btn-xs btn-success btn-ajax', url: 'goodsrecord/robot', success:function(){table.bootstrapTable('refresh', {});}},
                                {name: 'cancelRobot', text:'取消指定', title: '取消指定', classname:'btn btn-xs btn-danger btn-ajax', url: 'goodsrecord/cancelRobot', success:function(){table.bootstrapTable('refresh', {});}}
                            ],
                            events: Table.api.events.operate, 
                            formatter: function(value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if(row.status != 0 || row.is_robot == 1){
                                    $(table).data("operate-robot", null);
                                } else {
                                    $(table).data("operate-cancelRobot", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });



            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
